var currentTreeNode = null;
var treeObj;
var layer, form;
UserInfo.init();
var userName = UserInfo.userId;
var t = new Date();
var time = t.format('yyyy-MM-dd HH:mm:ss');

$(document).ready(function () {
    layer = layui.layer;
    $("#edit_node").css("display", "none");
    initTree();
    addEvent(document.getElementById('addBaseBtn'), 'click', addBaseFun);
    addEvent(document.getElementById('addPeerBtn'), 'click', addPeerFun);
    addEvent(document.getElementById('addChildBtn'), 'click', addChildFun);
    addEvent(document.getElementById('removeBtn'), 'click', removeFun);
    addEvent(document.getElementById('refreshBtn'), 'click', refershTree);
});

layui.use(["form"], function () {
    form = layui.form;
    form.verify({
        codeName: function(value, item) {
            if (!value || value.length == 0) {
                return '【字典名】是必须入力项目，不能为空。'
            }
        }
        ,codeValue: function(value, item) {
            if (!value || value.length == 0) {
                return '【字典值】是必须入力项目，不能为空。'
            }
            if(!(/^[1-9][0-9]*$/.test(value))){
                return '【字典值】是数字项目，请输入数字。'
            }
        }
        ,codeLevel: function(value, item) {
            if (!value || value.length == 0) {
                return '【字典层级】是必须入力项目，不能为空。'
            }
            if(!(/^(0|[1-9][0-9]*)$/.test(value))){
                return '【字典层级】是数字项目，请输入数字。'
            }
        }
        ,codeSort: function(value, item) {
            if(value && value.length > 0 && !(/^[1-9][0-9]*$/.test(value))){
                return '【顺序】是数字项目，请输入数字。'
            }
        }
    })

    form.on('submit(sub)', function (data) {
        console.log(JSON.stringify(data))
        saveData(data);
    })
});

/***
 * 加载树
 * */
function initTree() {
    $("#Tree").html("");
    listTree.load("Tree", {});
}

/*
* @author: wj
* @description: 字典树选中事件
* @date: 2018/12/21 17:23
* @param
* @return
*/
function treeselected(e) {
    var tree = e.sender;
    var node = e.node;
    currentTreeNode = node;
    console.log(JSON.stringify(node))
    $("#id").val(node.id);
    $("#codeName").val(node.codeName);
    $("#codeSort").val(node.codeSort);
    $("#codeRemark").val(node.codeRemark);
    $("#codeValue").val(node.codeValue);
    $("#codeLevel").val(node.codeLevel);
    $("#codeTypeName").val(node.codeTypeName);
    $("#codeIsParent").val(node.codeIsParent);
    $("#parentId").val(node.parentId);
    $("#codeRoot").val(node.codeRoot);
    $("#codeType").val(node.codeType);
    $("#codeShow").val(node.codeShow);
    $("#codePath").val(node.codePath);
    $("#codeCommon").val(node.codeCommon);
    if (node.codeIsParent == 1) {
        $("#yes").prop("checked", "checked");
    } else {
        $("#no").prop("checked", "checked");
    }
    if (node.codeCommon == 1) {
        $("#yes2").prop("checked", "checked");
    } else {
        $("#no2").prop("checked", "checked");
    }
    if (node.parentId != null && node.parentId != "") {
        ActHttpRequest("/sys-code/findById", {"id": node.parentId}, function (data) {
            if (data.OK == "OK") {
                $("#parentName").val(data.sysCode.codeName);
            }
        })
    } else {
        $("#parentName").val("没有父级节点");
    }

    $("#edit_node").css("display", "block");
    form.render();
}

/***
 * 保存数据
 * */
function saveData(data) {

    var formData = data.field;
    console.log(JSON.stringify(formData))
    ActHttpRequest("/sys-code/updateCode", formData, function (data) {
        if (data.OK == "OK") {
            refershTree();
            layer.msg("保存成功！", {icon: 1});
        } else {
            layer.msg(data.message, {icon: 5});
        }
    }, null, null, null, '保存中。。。', true);

    $.ajax({
        url: "/sys-code/updateCode",
        type:"post",
        data:formData,
        done:fun
    })
}

//添加根节点
function addBaseFun() {
    var data = {
        'codeName': 'newCode',
        'codeIsParent': 1,
        'codeCommon': 1,
        'codeSort': 0,
        'codeRoot': 1,
        'createUser': userName,
        'createTime': time
    };
    ActHttpRequest("/sys-code/addNewCode", data, function (d) {
        if (d.OK == "OK") {
            refershTree();
            layer.msg('添加成功！', {icon: 1});
        }
    })
}

//添加同级节点
function addPeerFun() {

    if (currentTreeNode != null && currentTreeNode.id != null) {
        var data = {
            'codeName': 'newCode',
            'codeSort': 0,
            'codeIsParent': currentTreeNode.codeIsParent,
            'codeCommon': 1,
            'parentId': currentTreeNode.parentId,
            'codeRoot': currentTreeNode.codeRoot,
            'createUser': userName,
            'createTime': time
        };

        ActHttpRequest("/sys-code/addNewCode", data, function (d) {
            if (d.OK == "OK") {
                refershTree();
                layer.msg('添加成功！', {icon: 1});
            }
        })
    } else {
        layer.open({
            title: '提示',
            icon: 0,
            content: '请选择节点！'
        })
    }
}

//添加子节点
function addChildFun() {
    if (currentTreeNode != null && currentTreeNode.id != null) {
        var data = {
            'codeName': 'newCode',
            'codeSort': 0,
            'codeIsParent': 2,
            'codeCommon': 1,
            'parentId': currentTreeNode.id,
            'codeRoot': 2,
            'codeTypeName': currentTreeNode.codeTypeName,
            'codeType': currentTreeNode.codeType,
            'createUser': userName,
            'createTime': time
        };
        ActHttpRequest("/sys-code/addNewCode", data, function (d) {
            if (d.OK == "OK") {
                refershTree();
                layer.msg('添加成功！', {icon: 1});
            }
        })
    } else {
        layer.open({
            title: '提示',
            icon: 0,
            content: '请选择节点！'
        })
    }
}

//删除节点
function removeFun() {
    if (currentTreeNode.codeRoot && currentTreeNode.codeRoot == 1) {
        layer.alert("根节点不能删除！", {icon: 5});
        return;
    } else {
        if (currentTreeNode != null && currentTreeNode.id != null) {
            layer.confirm("您确定要删除'" + currentTreeNode.codeName + "'吗？", {
                    btn: ['确定', '取消'] //按钮
                },
                function () {
                    ActHttpRequest("/sys-code/delCode", {"parentId": currentTreeNode.id}, function (data) {
                        if (data.OK == "OK") {
                            refershTree();
                            layer.msg('删除成功！', {icon: 1});
                        } else {
                            layer.alert(data.message, {icon: 5});
                        }
                    }, null, null, null, '删除中。。。', true);
                },
                function () {

                });
        } else {
            layer.open({
                title: '提示',
                icon: 0,
                content: '请选择节点！'
            })
        }
    }
}

function refershTree() {
    initTree();
    $("#edit_node").css("display", "none");
}